﻿
/*
 * Javasript based on Functions(JBF) 1.03
 * 
 * Copyright(c) 2010, Fooxee (JA86.CN)
 *
 * JBF is licensed under the terms of the Open Source GPL 3.0(http://www.gnu.org/licenses/gpl.html) license. 
 * 
 * JBF's blog: http://www.cnblogs.com/JBF/
 * 
 * Created by Fooxee. at 2010.03.08  mailTo:hjccjun@163.com
 */

//DOM相关操作
JBF.DomHelper = function(selector,context)
{
	
};

JBF.DomHelper.fn={
	
	//document方法
	//返回指定结点的引用
	//Node
	getElementById:function(id)
	{
		return getElementById(id);
	},
	//返回文档中所有匹配的元素的集合
	//NodeList
	getElementsByTagName:function(tagname)
	{
		return getElementsByTagName(tagname);
	},
	//创建节点
	//Node
	createElement:function(tagname)
	{
		return createElement(tagname);
	},
	//创建一个纯文本结点 
	//Node
	createTextNode:function(text)
	{
		return createTextNode(text);	
	},
	//NodeList
	find:function(selector,context,filter)
	{
		var arr=[];
		if(!context.nodeType)
		{
			return [];//debug
		}
		filter=JBF.isFunction(filter)?filter:function(){return true;};
		
		if (selector.nodeType)
		{
			arr.push(selector);
		}
		else if(typeof selector == "string")
		{
			var sels=selector.split(' ');
			if(JBF.len(sels)==1)
			{
				var match = /^#(\w+)$/.exec(sels[0]);
				if(match && match[1])
				{
					var el=context.getElementById(match[1]);
					if(el && '#'+el.id==selector)
						arr.push(el);
				}
				else
				{
					JBF.each(context.getElementsByTagName(sels[0]),function(v,i,list)
					{
						arr.push(v);
					});
				}
			}
			//#id p
			else
			{
				var match = /^#(\w+)$/.exec(sels[0]);
				if(match && match[1])
				{
					var el=context.getElementById(match[1]);
					if(el && '#'+el.id==sels[0])
					{
						var expr=selector.substr(JBF.len(sels[0]),JBF.len(selector));//alert(expr.trim().length)
						JBF.each(this.find(expr.trim(),el),function(v,i,list)
						{
							arr.push(v);
						});
					}
				}
				else
				{
					JBF.each(context.getElementsByTagName(sels[0]),function(v,i,list)
					{
						var expr=selector.substr(JBF.len(sels[0]),JBF.len(selector));
						JBF.each(this.find(expr.trim(),v),function(v2,i,list)
						{
							arr.push(v2);
						});
					},this);
				}
			}
		}
		JBF.each(arr,function(v,i,list)
		{
			if(!v || !filter(v))
			{
				try
				{
					arr.pop(v);
				}catch(e){JBF.debug(e)}
			}
		});
		return arr;

	}
	
	/*
	//nodes
	select:function(selector,context)
	{
		
	},
	createElement:function()
	{
		
	},
	append:function()
	{
		
	},
	appendTo:function()
	{
		
	},
	prepend:function()
	{
		
	},
	prependTo:function()
	{
		
	},
	after:function()
	{
		
	},
	insertAfter:function()
	{
		
	},
	before:function()
	{
		
	},
	insertBefore:function()
	{
		
	},
	removeChild:function()
	{
		
	},
	remove:function()
	{
		
	},
	repalceChild:function()
	{
		
	},
	setAttribute:function()
	{
		
	},
	getAttribute:function()
	{
		
	},
	eq:function()
	{
		
	}
	*/
}
